﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mds.Common
{
    public class ServiceFactory : IServiceFactory
    {
        private Dictionary<String, Object> serviceCache;
        private static ServiceFactory _instance = new ServiceFactory();
        public static ServiceFactory Intance {
            get { return _instance; }
        }
        private ServiceFactory()
        {
            serviceCache = new Dictionary<string, object>();
        }
        public T CreateInstance<T>(String serviceName)
        {
            if (serviceCache.ContainsKey(serviceName))
            {
                return (T)serviceCache[serviceName];
            }
            else {
                var serviceIntance = (T)ClassReflection.CreateIntanceSafe(ClassConfigurationCache.Instance.GetServiceClass(serviceName));
                serviceCache.Add(serviceName, serviceIntance);
                return serviceIntance;
            }
        }
    }
}
